import {
  Body,
  Controller,
  Get,
  HttpException,
  HttpStatus,
  Inject,
  Logger,
  Post,
  Req,
  Request,
  Res,
  Session,
  UnauthorizedException,
  UseGuards,
} from '@nestjs/common';
import { ToolsService } from 'common/lib/utils/tools.service';

@Controller('verification')
export class VerificationCodeController {
  @Inject()
  private readonly toolsService: ToolsService;

  private readonly logger = new Logger(VerificationCodeController.name);

  constructor() {}

  @Get('code') //当请求该接口时，返回一张随机图片验证码
  async getCode(@Req() req, @Res() res) {
    const svgCaptcha = await this.toolsService.captche(); //创建验证码
    req.session.code = svgCaptcha.text; //使用session保存验证，用于登陆时验证
    res.type('image/svg+xml'); //指定返回的类型
    res.send(svgCaptcha.data); //给页面返回一张图片
  }
}
